using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Sedna.Util
{
    public class Converter
    {
        public static Hashtable DataTableToHashtable(DataTable input)
        {
            Hashtable result = new Hashtable(input.Rows.Count);
            return null;
        }

        public static String ByteArrayToString(byte[] data)
        {
            StringBuilder result = new StringBuilder(data.Length * 4);
            for (int i = 0; i < data.Length; i++)
            {
                result.Append(data[i].ToString());
                result.Append(';');
            }
            return result.ToString();
        }

        public static byte[] StringToByteArray(String data)
        {
            byte[] result = new byte[data.Length];
            for (int i = 0; i < data.Length; i++)
                result[i] = (byte)data[i];
            return result;
        }

        public static byte[] SubArray(byte[] array, int start, int end)
        {
            byte[] result = new byte[end - start + 1];
            for (int i = 0; i < end - start + 1; i++)
                result[i] = array[start + i];
            return result;
        }
        
        public static bool ToBoolean(object value)
        {
            if (value == null)
                return false;

            string str = value.ToString().Trim().ToLower();

            if (str.Length == 0 ||
                "false".Equals(str) ||
                "n".Equals(str))
            {
                return false;
            }
            
            if ("true".Equals(str) ||
                "y".Equals(str))
            {
                return true;
            }

            long num;

            if (long.TryParse(str, out num))
                return (num > 0);

            throw new ArgumentException(String.Format("Failed to convert the value '{0}' to boolean", value));
        }
    }

}
